*******************************************************************************
* Specify city; load data
*******************************************************************************

local city = "east_baton_rouge"

use dataset_`city'_panel_pres, clear

*******************************************************************************
* Rescale explanatory variables
*******************************************************************************

foreach x in pctnhblack distance {
	su `x'
	replace `x' = (`x'- r(min)) / (r(max) - r(min))
}
replace distance = -distance + 1

*******************************************************************************
* Time interactions
*******************************************************************************

tabulate year, gen(year_)
gen pctnhblackXy2 = pctnhblack*year_2
gen pctnhblackX2020 = pctnhblack*year_3
gen distanceXy2 = distancem*year_2
gen distanceX2020 = distancem*year_3

label variable pctnhblackXy2 "Share Black X 2016"
label variable pctnhblackX2020 "Share Black X 2020"
label variable distanceXy2 "Proximity to Shooting X 2016"
label variable distanceX2020 "Proximity to Shooting X 2020"

*******************************************************************************
* Do the regressions
*******************************************************************************

eststo clear

eststo: reg pctdem pctnhblackXy2 pctnhblackX2020 ///
	distanceXy2 distanceX2020 year_2 year_3 ///
	[aw=totalvotes], robust cl(precinct) absorb(precinct)
unique precinct if e(sample)
estadd local nclust = r(unique)
estadd local yfe = "X"
estadd local pfe = "X"
estadd local wtvote = "X"
estadd local myse = "Precinct-"
estadd local myse2 = "clustered"

foreach l in 1 2 5 {
	eststo: xi: acreg pctdem pctnhblackXy2 pctnhblackX2020 ///
		distanceXy2 distanceX2020 year_2 year_3 ltotalvotes, ///
		pfe1(id) spatial latitude(y_c) longitude(x_c) ///
		distcutoff(`l') hac id(precinct) time(year) lagcutoff(21)
	estadd local slag = `l'
	unique precinct if e(sample)
	estadd local nclust = r(unique)
	estadd local yfe = "X"
	estadd local pfe = "X"
	estadd local wtvote = ""
	estadd local clvote = "X"
	estadd local myse = "HAC"
	estadd local myse2 ""
}

*******************************************************************************
* Output table
*******************************************************************************

#delimit;

esttab
	using "_output/table A27.tex"
	,
		replace
		booktabs
		label
		nomtitles
		collabels(none)
		drop(ltotalvotes year_* _cons)
		cells(b(fmt(3) star) se(par))
		width(\linewidth)
		stats(yfe pfe wtvote clvote myse myse2 slag N nclust, 
			label(
				"Year Fixed Effects"
				"Precinct Fixed Effects"
				"Weight by Total Votes Cast"
				"Control for Log(Total Votes Cast)"
				"Standard Errors"
				" "
				"Distance Cutoff (Miles)"
				"Number of Observations"
				"Number of Unique Precincts" 
			)
			fmt(%20.0f %20.0f))
		compress
		gap
		alignment(l)
		;
			
#delimit cr	

*******************************************************************************
* End
*******************************************************************************
